﻿using System;
using System.Reflection;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using WmsService.BusinessLogic;

namespace WmsService.AOP
{
    public class InvoiceBillInStoreBillDtlHistoryCallHandler : ICallHandler
    {
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            //开始获取实体
            string inStoreBillDtlID= null;
            string invoiceNo = null;
            InvoiceBillInStoreBillDtl invoiceBillInStoreBillDtl = null;

            if(input.Arguments.Count != 2)
            {
                throw new AopException("[InvoiceBillInStoreBillDtlHistoryCallHandler] 特性绑定方法错误：" + input.MethodBase.Name);
            }

            invoiceNo = (String)input.Arguments[0];
            inStoreBillDtlID = (String)input.Arguments[1]; 
           

            //var logic = (IInvoiceAndPayApplyBillLogic)input.Target;
            dynamic logic = input.Target;
            invoiceBillInStoreBillDtl = logic.GetInvoiceBillInStoreBillDtl(invoiceNo, inStoreBillDtlID);
            
            //执行方法，如果没有异常，就执行下面的保存历史记录方法
            var methodReturn = getNext()(input, getNext);

            var invoiceBillInStoreBillDtlHistory = InvoiceBillInStoreBillDtlHistory.
                CreateInvoiceBillInStoreBillDtlHistory(0,
                invoiceBillInStoreBillDtl.InvoiceNo,
                invoiceBillInStoreBillDtl.InStoreBillDtlID,
                invoiceBillInStoreBillDtl.Tag,
                invoiceBillInStoreBillDtl.InvoiceDtlAmountTax);

            var propertyInfos = invoiceBillInStoreBillDtl.GetType().GetProperties();

            PropertyInfo pi;
            foreach (var propertyInfo in propertyInfos)
            {
                pi = typeof(InvoiceBillInStoreBillHistory).GetProperty(propertyInfo.Name);
                if(pi != null && pi.Name != "EntityKey")
                {
                    try
                    {
                        pi.SetValue(invoiceBillInStoreBillDtlHistory, propertyInfo.GetValue(invoiceBillInStoreBillDtl, null), null);
                    }
                    catch (Exception)
                    {}
                }
            }

            invoiceBillInStoreBillDtlHistory.OperationTime = DateTime.Now;
            invoiceBillInStoreBillDtlHistory.ExtVarChar3 = "删除该记录";
            var logicBase = (LogicBase)input.Target;
            invoiceBillInStoreBillDtlHistory.ExtVarChar4 = logicBase.ContextUserID;

            var ctx = Factory.ContextFactory.Create();
            ctx.AddToInvoiceBillInStoreBillDtlHistory(invoiceBillInStoreBillDtlHistory);
            ctx.SaveChanges();

            return methodReturn;
        }
        public int Order { get; set; }
    }

    public class InvoiceBillInStoreBillDtlHistoryCallHandlerAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new InvoiceBillInStoreBillDtlHistoryCallHandler { Order = this.Order };
        }
    }
}